package com.jzhao.jpre.model.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;
import com.jzhao.jpre.base.StringPublicDeal;
import com.jzhao.jpre.model.PospMessageLog;


public class PospMessageLogDao 
{
	protected JdbcTemplate jdbcTemplate;
	protected StringPublicDeal strPubDeal;
	protected LobHandler lobHandler;	
	protected LobCreator lobCreator;

	public void setDataSource(DataSource dataSource) 
	{
		jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public void setStrPubDeal(StringPublicDeal strPubDeal) 
	{
		this.strPubDeal = strPubDeal;
	}
	
	public void setLobHandler(LobHandler lobHandler) {
		this.lobHandler = lobHandler;
	}

	public int insert(final PospMessageLog jour)
	{
		/*String sql = "INSERT INTO POSP_MESSAGE_LOG ("
	        +"ID,"	//-------------------------1
	        +"POSID,"	//---------------------2
	        +"MASSAGE," //---------------------3
	        +"VALIDATEKEY,"	//-----------------4
	        +"FLATTIME," //--------------------5
	        +"FLAG) "	//---------------------6
	        +" VALUES( "
	        +"?,?,?,?,TO_DATE(?,'YYYYMMDDHH24MISS'),?)";ORACLE*/
		
		final String sql = "INSERT INTO POSP_MESSAGE_LOG ("
//	        +"ID,"	//-------------------------1
	        +"POSID,"	//---------------------2
	        +"MASSAGE," //---------------------3
	        +"VALIDATEKEY,"	//-----------------4
	        +"FLATTIME," //--------------------5
	        +"FLAG) "	//---------------------6
	        +" VALUES( "
//	        +"?,?,?,?,str_to_date(?,'%Y%M%D%H%i%S'),?)";
	        +"?,?,?,now(),?)";//MYSQL
		
//		jdbcTemplate.execute( sql,
//				new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) {			
//				protected void setValues(PreparedStatement ps, LobCreator lobCreator)
//						throws SQLException, DataAccessException {
//					
//					ps.setLong(1, jour.getId());
//					ps.setLong(2, jour.getPosid());
//					
//					lobCreator.setBlobAsBytes(ps, 3,strPubDeal.convToByteFrom8859(jour.getMassage()));
//					
//					ps.setString(4, jour.getValidatekey());
//					ps.setString(5, jour.getFlattime());
//					ps.setInt(6, jour.getFlag());
//				}
//			});		
		
		
		 KeyHolder keyHolder = new GeneratedKeyHolder();
			jdbcTemplate.update(
			       new PreparedStatementCreator(){
			           public PreparedStatement createPreparedStatement(Connection conn) throws SQLException{
			               int i = 0;
			               PreparedStatement ps = conn.prepareStatement(sql, new String[] {"object_id"});
			               
			               ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			               ps.setLong(++i, jour.getPosid());
			               ps.setString(++i, jour.getMassage());
			               ps.setString(++i, jour.getValidatekey());
//			               ps.setString(++i, jour.getFlattime());
			               ps.setInt(++i, jour.getFlag());
			               return ps;
			           }
			       },
			       keyHolder);
			
			System.out.println("id = " + keyHolder.getKey());
			return keyHolder.getKey().intValue();
	}	

}
